//
//  Q&AViewController.m
//  xpkc
//
//  Created by TopSageOSX on 14/12/29.
//  Copyright (c) 2014年 wanmeizty. All rights reserved.
//

#import "Q&AViewController.h"
#import "UserMessage.h"
#import "myAskModel.h"
#import "MBProgressHUD.h"
@interface Q_AViewController ()

@end

@implementation Q_AViewController{
    NSArray *myQuestion;
    NSArray *myAnswer;
    UITableView *QATable;
    NSMutableArray *_dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的答疑";
    _dataArray=[NSMutableArray array];
    self.navigationController.navigationBar.hidden = NO;
    [self creatQAView];
    [self createData];
}
- (void)viewDidAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}
-(void)createData
{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"加载中";
    //获取数据
    NSString *module = @"myCourse";
    NSString *interface = @"myasklist";
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    
    NSDictionary *parameter = @{@"time":timeString,@"mid":[UserMessage sharedUserMessage].userMid};
    //POST请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
    
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
       
        NSDictionary *dics = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
      
     if (dics.count==0) {
            
         ShowAlertMsg(@"暂无消息");
         
            
        }else{
        
        NSArray * arr =[dics objectForKey:@"AskMsg"];
//        NSArray * arr1 =[NSArray array];
        for (NSDictionary *dic in arr) {
//            arr1 = [dic objectForKey:@"ReplyList"];
//            for (NSDictionary * dic1 in arr1) {
            myAskModel * model =[[myAskModel alloc] init];
            model.asktime=[dic objectForKey:@"asktime"];
            model.myaskcontent=[dic objectForKey:@"myaskcontent"];
            model.myasktitle=[dic objectForKey:@"myasktitle"];
            model.myname=[dic objectForKey:@"myname"];
            model.ReplyList = [dic objectForKey:@"ReplyList"];
//                model.username=[dic1 objectForKey:@"username"];
//                model.replytime=[dic1 objectForKey:@"replytime"];
//                model.content=[dic1 objectForKey:@"content"];
           
                [_dataArray addObject:model];
//            }
    }
            
        
        [QATable reloadData];
            [hud hide:YES];
        
        }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        logdebug(@"%@",error.localizedDescription);
        [hud hide:YES];
    }];
    
}

- (void)creatQAView{
    self.view.backgroundColor = BGColor;
    QATable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-60)];
    QATable.delegate = self;
    QATable.dataSource = self;
    QATable.separatorStyle = UITableViewCellSeparatorStyleNone;
    QATable.backgroundColor = [UIColor clearColor];
    [self.view addSubview:QATable];
    

}




#pragma mark == tableview delegate ==
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    myAskModel * model =_dataArray[indexPath.row];
    CGFloat offsize = 0.04*SCREEN_WIDTH;
    NSMutableString * mutStr = [NSMutableString stringWithString:model.myaskcontent];
    [mutStr replaceOccurrencesOfString:@"<p>" withString:@"\n\t" options:NSCaseInsensitiveSearch range:NSMakeRange(0, model.myaskcontent.length - 1)];
    
    [mutStr replaceOccurrencesOfString:@"</p>" withString:@"\n" options:(NSCaseInsensitiveSearch) range:NSMakeRange(0, mutStr.length - 1)];
    
    [mutStr replaceOccurrencesOfString:@"<br/>" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, mutStr.length - 1)];
    CGSize contentSize = [mutStr sizeWithFont:FONT_SYS_M constrainedToSize:CGSizeMake(SCREEN_WIDTH-offsize - offsize * 2 , 2000)];
    
    if (contentSize.height > 55) {
        
         return 120 + contentSize.height - 55 + 30;
        
    }
    
    return 120;
}
- (QAViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QAViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[QAViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    myAskModel * model =_dataArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell configWithAskModel:model];
//    cell.questionStr = model.myaskcontent;
//    cell.timeStr =model.asktime;
//    cell.myname.text=model.myname;
//    cell.myasktitle.text=model.myasktitle;
//    if (model.ReplyList.count) {
//        cell.msgState=MsgStateRecive;
//    }else{
//    cell.msgState = MsgStateWait;
//    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Q_AInfoViewController *QAIVC = [[Q_AInfoViewController alloc]init];
    myAskModel * model =_dataArray[indexPath.row];
    QAIVC.replyList=model.ReplyList;
//    QAIVC.myasktitle=model.myasktitle;
//    QAIVC.myaskcontent=model.myaskcontent;
//    QAIVC.myname=model.myname;
//    QAIVC.asktime=model.asktime;
    QAIVC.askModel = model;
    [self.navigationController pushViewController:QAIVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
